package com.simple.editor.action.impl;

import com.simple.editor.ApplicationContext;
import com.simple.editor.callback.DefaultUICallbackImpl;
import com.simple.editor.converter.FileModelConverter;
import com.simple.editor.ui.FileModel;
import com.simple.editor.ui.InfoPane;
import com.simple.editor.vcs.VcsClient;
import com.simple.editor.vcs.VcsClientRegistry;
import com.simple.editor.vcs.VcsFile;

import java.util.Map;

/**
 * {@link RevertEditorUIAction} handles user "Revert File" action.
 * <p/>
 * Created with IntelliJ IDEA.
 * User: vsoroka
 * Date: 07.06.13
 * Time: 11:52
 * To change this template use File | Settings | File Templates.
 */
public class RevertEditorUIAction extends AbstracEditorUIAction {

    private final ApplicationContext ctx;
    private final InfoPane infoPane;
    private final VcsClientRegistry vcsClientRegistry;

    public RevertEditorUIAction(final ApplicationContext ctx, final InfoPane infoPane, final VcsClientRegistry vcsClientRegistry) {
        this.ctx = ctx;
        this.infoPane = infoPane;
        this.vcsClientRegistry = vcsClientRegistry;
    }

    @Override
    public void perform(final FileModel... files) {
        if (files != null && files.length > 0) {
            final VcsClient vcsClient = vcsClientRegistry.getCurrentVcsClient();
            final Map<String, VcsFile> vcsFilesMap = FileModelConverter.convertFiles(files);

            // Revert VCS files
            vcsClient.revert(new DefaultUICallbackImpl("Revert files", infoPane), vcsFilesMap.values());

            // Refresh VCS files status
            vcsClient.refreshStatus(new RefreshActionUICallbackImpl(vcsFilesMap, infoPane, ctx, true, files), vcsFilesMap.values());
        }
    }
}
